/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.project.actions.publicAction;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import org.apache.tools.ant.module.api.support.ActionUtils;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.execution.ExecutorTask;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
import pl.put.dcs.core.utils.serializer.SerializeManager;
import pl.put.dcs.core.workflow.serializer.WorkflowSerializer;
import pl.put.dcs_modeler.exceptions.actions.PublicationException;
import pl.put.dcs_modeler.project.projectPropertiesExtensions.dcsProjectProperties.projectConfigModel.DCSServerConfig;
import pl.put.dcs_modeler.utils.DCSProjectUtil;
import pl.put.dcs_modeler.utils.FlowModelUtil;
import pl.put.model.flowModel.FlowModel;
import pl.put.model.flowModel.SerializableFlowModel;
/**
 *
 * @author LukaszR
 */
public class PublicOnLocalServerAction extends PublicAction
{
    public static Logger log = Logger.getLogger( PublicOnLocalServerAction.class.getName() );

    private Project project;
    private String path;

    public PublicOnLocalServerAction( Project project, String path )
    {
        this.project = project;
        this.path = path;
    }



    @Override
    public void perform() throws PublicationException
    {
        try
        {
            // ustalenie sciezki do serializedFlowModel
            String sfmPath = FlowModelUtil.getSerializedFlowModelPath( project );
            log.info( "Serialized flow model path: " + sfmPath );
            log.info( "project directory: " + project.getProjectDirectory().getPath() );
            //File file = new File( path );
            FileObject fo = project.getProjectDirectory().getFileObject( sfmPath );
            InputStream is = fo.getInputStream();
            SerializeManager sm = new SerializeManager();
            sm.init();
            SerializableFlowModel sfm = ( SerializableFlowModel )sm.unSerializeFromFile( is );
            FlowModel flowModel = sfm.getFlowModel();
            WorkflowSerializer.serializeFlowModelRemoteToFile( flowModel, project );
            // ustawienie propertisow do przekazania
            Properties props = new Properties();
            props.setProperty( "projName", ProjectUtils.getInformation( project ).getDisplayName() );
            List< DCSServerConfig > svrs = DCSProjectUtil.getServersConfig( project );
            String serverPath = "";
            if ( ( svrs.size() > 0 ) && ( !svrs.get( 0 ).getServerPath().trim().equals( "" ) ) )
            {
                serverPath = svrs.get( 0 ).getServerPath();
            }
            else
            {
                throw new PublicationException( "PublicOnLocalServer", "I can't public Your project. You have to set server publication dir path." );
            }
            props.setProperty( "deploydir", serverPath );
            // pobranie pliku ant-a
            FileObject projTmp = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/DCS/DCS Project" );
            // pobranie objektu skryptu ant-a
            FileObject ant = project.getProjectDirectory().getFileObject( ( String )projTmp.getAttribute( "deployAntPath" ) );
            ExecutorTask et = ActionUtils.runTarget( ant, new String[]{ "deploy" }, props );
            if ( et.result() > 0 )
            {
                throw new PublicationException( this.getClass().getName(), "I can't public project on local server" );
            }
        }
        catch ( IOException ex )
        {
            throw new PublicationException( this.getClass().getName(), ex.getMessage() );
        }
        catch ( IllegalArgumentException ex )
        {
            throw new PublicationException( this.getClass().getName(), ex.getMessage() );
        }
    }

}
